# -.- coding: utf-8 -.-

from django import forms
from models import *

class SendingForm(forms.ModelForm):
	class Meta:
		model = package
		fields = ('comment','package')

	def save(self, destinatario, sendr):
		#Pedimos a twitter el id del receptor en base a su nombre
		try:
			recvr = user.objects.get(id_twitter=destinatario.id)
		except user.DoesNotExist:
			django_user = User(
				username = destinatario.screen_name,
			)
			django_user.set_password("Empty")
			django_user.save()

			recvr = user(
				user = django_user,
				username = destinatario.screen_name,
				id_twitter = destinatario.id,
				access_token = "",
				access_secret = "",
			)
			recvr.save()

		paq = package()
		paq.comment = self.cleaned_data['comment']
		paq.package = self.cleaned_data['package']
		paq.id_receiver = recvr
		paq.id_sender = sendr
		paq.original_file = ""
		paq.save()

		return paq.id